const app = getApp()
import Common from '../../common/common.js';
let common = new Common;
Page({
  data: {
    list: [],
    pageNumber: 1,
    size: 10,
    isEnd: false, //是否到底
    search_key: '',
    orgSeq: '',//店铺id 
    userType: '',
    userId: '',
    payMethod: 9,
    orderSources: 1,//1,线上商城 7定制酒
    orderStatus: 2,//单个传参
    isGiftOrder: null,//含赠品订单参数
    showPrewImage: false
  },
  //查看
  previewImage(e) {
    let imageurl = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [imageurl],
      current: imageurl,
      success: res => {
        console.log('预览')
      }
    })
  },
  hideShows() {
    this.setData({
      imageurl: '',
      showPrewImage: false
    })
  },
  // 商家确认收货（定制酒）
  confirmGoods(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success(res) {
        if (res.confirm) {
          let data = {
          }
          common.requestAjax('/api/trade-service/v1/tradeNotification/initCustomizeStatus?uuid=' + e.currentTarget.dataset.uuid + "&customizeStatus=" + e.currentTarget.dataset.customizestatus, 'POST', data).then(res => {
            if (res.data.code == 1) {
              //修改数组中对象的值
              let index = e.currentTarget.dataset.indexs
              let customizeStatus = `list[${index}].customizeStatus`
              that.setData({
                [customizeStatus]: 7,
              })
              wx.showToast({
                title: '您已确认收货',
                icon: 'success'
              })
            } else {
              common.showToast(res.data.message)
            }
          }).catch(err => {
            console.log(err);
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 切换普通和定制订单
  changeType(e) {
    this.setData({
      orderSources: e.currentTarget.dataset.type
    })
    this.getProductList(1, 1);
  },

  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  info: function (e) {
    let routeUrl = '/pages/myOrderMgt/orderInfo/orderInfo';
    let routeType = 'navigateTo';
    let params = '?type=' + this.data.orderStatus + '&id=' + e.currentTarget.dataset.id;
    common.routeLink(routeUrl, params, routeType)
  },
  onPullDownRefresh: function () {
    //清空搜索框
    this.setData({
      value: '',
      pageNumber: 1,
      isEnd: false
    })
    this.getProductList(1, 1)
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },
  onReachBottom: function () {
    if (!this.data.isEnd) {
      let pageNumber = this.data.pageNumber;
      pageNumber += 1;
      this.getProductList(pageNumber, 2)
      wx.showLoading({
        title: '正在加载中...',
      })
      this.setData({
        pageNumber
      })
    }
  },

  searchList: function (e) {
    this.setData({
      search_key: e.detail.value
    })
    this.getProductList(1, 1)
  },
  getProductList(page, type) {
    let data = {
      condition: {
        userId: this.data.userId,
        orderStatus: this.data.orderStatus,//0待付款 1待发货 2待收货 3完成 4取消 9全部
        keyWords: this.data.search_key,
        returnStatus: 9, //是否是退单 0正常单 
        payMethod: this.data.payMethod,
        orderSources: this.data.orderSources,
        // dxOrderStatus: this.data.dxOrderStatus,
        isGiftOrder: this.data.isGiftOrder//赠品订单参数
      },
      current: page,
      size: 10,
    }

    common.requestAjax('/api/trade-service/v1/myorder/queryMyOrders', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.hideLoading()
        let list = this.data.list || [];
        let isEnd = this.data.isEnd;
        if (type == 1) {
          //下拉刷新
          list = res.data.records;
          isEnd = false;
        } else if (type == 2) {
          //上拉加载
          list = list.concat(res.data.records);
          if (res.data.records.length == 0) {
            //最后一行没数据 到底了
            isEnd = true;
          }
        }
        this.setData({
          list: list,
          isEnd,
        })
      } else {
        common.showToast(res.data.message)
      }
    }).catch(err => {
      console.log(err);
    })
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      orgSeq:app.globalData.orgSeq,
      userType:app.globalData.shopInfo.userType,
      userId:app.globalData.userId,
    })
  },
  onShow: function () {
    this.getProductList(1, 1)
  },
  changeOrder(e){
    this.setData({
      search_key:'',
      orderStatus:Number(e.currentTarget.dataset.active)
    })
    this.getProductList(1,1);
  }
})